// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Author: Markus Gutschke

#ifndef XML_H__
#define XML_H__

#include <stdlib.h>

#include "hashmap.h"


enum XMLType { X_ELEMENT, X_CDATA };

struct XMLElement {
  struct XMLElement *parent;
  struct HashMap    ns;
  char              *namespace;
  enum XMLType      type;
  union {
    char            *element;
    char            *cdata;
  };
  struct HashMap    attributes;
  struct XMLElement **children;
  size_t            children_size;
};

struct XMLState {
  int               doc;
  char              *buf;
  size_t            len, sz;
  struct XMLElement *element;
  char              *tmpBuf;
  size_t            tmpSz;
};

struct XML {
  struct XMLState state;
  struct HashMap  *ns;
  struct HashMap  namespaces;
  struct HashMap  elements;
  struct HashMap  attributes;
  struct HashMap  misc;
};

struct XML *newXML();
void initXML(struct XML *xml);
void destroyXML(struct XML *xml);
void deleteXML(struct XML *xml);
void resetXML(struct XML *xml);
size_t parseXML(struct XML *xml, const char *txt, size_t len,
              int (*fnc)(struct XML *xml, struct XMLElement *elem, void *arg),
              void *arg);
char *xmlEscape(const char *s);
int xmlElementMatches(struct XMLElement *elem, const char *namespace,
                      const char *element);
int xmlElementHasChild(struct XMLElement *elem, struct XMLElement **child,
                       const char *namespace, const char *element,
                       const char *childs_cdata);
int xmlAttributeMatches(struct XMLElement *elem, const char *attr,
                        const char *value);

#endif  // XML_H__
